import 'package:boss/color_constant.dart';
import 'package:boss/ui/widget/timeRangePicker/provider/time_range_picker_provider.dart';
import 'package:boss/ui/widget/timeRangePicker/widget/time_range_picker_item.dart';
import 'package:hi_common/hi_common.dart';
import 'package:boss/util/time_util.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class TimeRangePickerView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.symmetric(
        horizontal: 30.w,
      ),
      child: Consumer<TimeRangePickerProvider>(
        builder: (context, provider, child) {
          List<Widget> rows = [];
          for (int i = 0; i < 5; i++) {
            List<Widget> cells = [];
            int offsetStart;
            int offsetEnd;

            for (int j = 0; j < 5; j++) {
              var index = i * 5 + j;

              if (provider.startTime != null && provider.endTime != null) {
                if (offsetStart == null && index >= provider.startTime && index < provider.endTime) {
                  offsetStart = j;
                }

                if (index <= provider.endTime && index != provider.startTime) {
                  offsetEnd = 4 - j;
                }
              }

              cells.add(TimeRangePickerItem(
                time: TimeUtil.int2TimeString(index),
                index: index,
                onTap: () {
                  var provider = Provider.of<TimeRangePickerProvider>(
                    context,
                    listen: false,
                  );
                  provider.setTime(index);
                },
              ));
            }

            double offsetWidth = (MediaQuery.of(context).size.width - ScreenUtil.instance.setWidth(60.0 + 100 * 5)) / 4 + 100.w;

            rows.add(Container(
              margin: EdgeInsets.symmetric(
                vertical: 20.w,
              ),
              child: Stack(
                children: <Widget>[
                  if (offsetEnd != null && offsetStart != null)
                    Positioned(
                      top: 0,
                      left: offsetWidth * offsetStart,
                      right: offsetEnd * offsetWidth,
                      child: Container(
                        height: 66.w,
                        decoration: BoxDecoration(
                          color: ColorConstant.f7184f5.withOpacity(0.9),
                          borderRadius: BorderRadius.circular(
                            66.w,
                          ),
                        ),
                      ),
                    ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: cells,
                  )
                ],
              ),
            ));
          }

          return Column(
            children: rows,
          );
        },
      ),
    );
  }
}
